home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Info-Mac 3
/
Info_Mac_1994-01.iso
/
Development
/
General
/
SpriteWorld 1.0b3
/
Examples
/
SimpleBreakOut
/
SimpleBreakOut.c
< prev
next >
Wrap
Text File
|
1993-06-20
|
10KB
|
417 lines
///--------------------------------------------------------------------------------------
// SimpleBreakOut.c
//
// By: Tony Myles
//
// Copyright © 1993 Tony Myles, All rights reserved worldwide.
///--------------------------------------------------------------------------------------
#ifndef __DESK__
#include <Desk.h>
#endif
#ifndef __EVENTS__
#include <Events.h>
#endif
#ifndef __OSEVENTS__
#include <OSEvents.h>
#endif
#ifndef __TOOLUTILS__
#include <ToolUtils.h>
#endif
#ifndef __SEGLOAD__
#include <SegLoad.h>
#endif
#ifndef __WINDOWS__
#include <Windows.h>
#endif
#ifndef __SPRITEWORLD__
#include <SpriteWorld.h>
#endif
#ifndef __SPRITELAYER__
#include <SpriteLayer.h>
#endif
#ifndef __SPRITE__
#include <Sprite.h>
#endif
#ifndef __FRAME__
#include <Frame.h>
#endif
#ifndef __SPRITEWORLDUTILS__
#include <SpriteWorldUtils.h>
#endif
#ifndef __APPLICATION__
#include "Application.h"
#endif
#ifndef __SIMPLEBREAKOUT__
#include "SimpleBreakOut.h"
#endif
///--------------------------------------------------------------------------------------
// PerformSimpleAnimation
///--------------------------------------------------------------------------------------
void PerformSimpleAnimation(CWindowPtr srcWindowP)
{
OSErr err;
PixPatHandle pixPatH;
SpriteWorldPtr spriteWorldP;
SpriteLayerPtr brickLayerP;
SpriteLayerPtr ballLayerP;
SpriteLayerPtr paddleLayerP;
SpritePtr brickSpriteArray[kTotalNumberOfBricks];
SpritePtr ballSpriteP;
SpritePtr paddleSpriteP;
long brickNum, ticks, frames;
short horizOffset, vertOffset, brickHoriz, brickVert, col, row;
Rect moveBoundsRect, worldRect;
SetPort((GrafPtr)srcWindowP);
SetCursor(*GetCursor(watchCursor));
//
// STEP #1: initialize the sprite world package
//
err = SWEnterSpriteWorld();
FatalError(err);
//
// STEP #2: create the various pieces that we need
//
// create the sprite world
err = SWCreateSpriteWorldFromWindow(&spriteWorldP, srcWindowP, NULL);
FatalError(err);
// create the brick layer
err = SWCreateSpriteLayer(&brickLayerP);
FatalError(err);
// create the ball layer
err = SWCreateSpriteLayer(&ballLayerP);
FatalError(err);
// create the paddle layer
err = SWCreateSpriteLayer(&paddleLayerP);
FatalError(err);
err = SWCreateSpriteFromCIconResource(brickSpriteArray, NULL, kBrickCIconID, 1, kFatMask);
// create brick sprites
for (brickNum = 1; brickNum < kTotalNumberOfBricks; brickNum++)
{
err = SWCloneSprite(brickSpriteArray[0], brickSpriteArray + brickNum, NULL);
FatalError(err);
}
// create ball sprite
err = SWCreateSpriteFromCIconResource(&ballSpriteP, NULL, kBreakBallCIconID, 1, kFatMask);
FatalError(err);
// create paddle sprite
err = SWCreateSpriteFromCIconResource(&paddleSpriteP, NULL, kPaddleCIconID, 1, kFatMask);
FatalError(err);
//
// STEP #3: put the pieces together (must be done BEFORE the sprite world is locked!)
//
for (brickNum = 0; brickNum < kTotalNumberOfBricks; brickNum++)
{
// add the brick sprite to the brick layer
SWAddSprite(brickLayerP, brickSpriteArray[brickNum]);
}
// add the ball sprite to the ball layer
SWAddSprite(ballLayerP, ballSpriteP);
// add the paddle sprite to the paddle layer
SWAddSprite(paddleLayerP, paddleSpriteP);
// add the layers to the world
SWAddSpriteLayer(spriteWorldP, brickLayerP);
SWAddSpriteLayer(spriteWorldP, ballLayerP);
SWAddSpriteLayer(spriteWorldP, paddleLayerP);
//
// STEP #4: lock the sprite world !!! VERY IMPORTANT !!!
//
SWLockSpriteWorld(spriteWorldP);
//
// STEP #5: set things up for the animation (must be done AFTER the sprite world is locked!)
//
// calculate the movement boundary rectangle
moveBoundsRect = srcWindowP->portRect;
horizOffset = (brickSpriteArray[0]->curFrameP->frameRect.right - brickSpriteArray[0]->curFrameP->frameRect.left) + 2;
vertOffset = (brickSpriteArray[0]->curFrameP->frameRect.bottom - brickSpriteArray[0]->curFrameP->frameRect.top) + 2;
brickHoriz = 2;
brickVert = (vertOffset * 2);
brickNum = 0;
// set up the sprites
for (col = 0; col < kNumberOfBrickColumns; col++)
{
brickVert = (vertOffset * 2);
for (row = 0; row < kNumberOfBrickRows; row++)
{
// set the sprite’s initial location
SWSetSpriteLocation(brickSpriteArray[brickNum], brickHoriz, brickVert);
SWSetSpriteMoveTime(brickSpriteArray[brickNum], -1);
brickNum++;
brickVert += vertOffset;
}
brickHoriz += horizOffset;
}
// set the ball’s movement characteristics
SWSetSpriteLocation(ballSpriteP, moveBoundsRect.left, moveBoundsRect.bottom - 100);
SWSetSpriteMoveBounds(ballSpriteP, &moveBoundsRect);
SWSetSpriteMoveDelta(ballSpriteP, 2, 2);
SWSetSpriteMoveTime(ballSpriteP, 6);
SWSetSpriteMoveProc(ballSpriteP, BallMoveProc);
SWSetSpriteCollideProc(ballSpriteP, BallCollideProc);
// set the paddle’s movement characteristics
SWSetSpriteLocation(paddleSpriteP, moveBoundsRect.left, moveBoundsRect.bottom - 30);
SWSetSpriteMoveProc(paddleSpriteP, PaddleMoveProc);
moveBoundsRect.right -= 32;
SWSetSpriteMoveBounds(paddleSpriteP, &moveBoundsRect);
SWSetSpriteCollideProc(paddleSpriteP, PaddleCollideProc);
SWSetPortToBackGround(spriteWorldP);
worldRect = srcWindowP->portRect;
if (SWHasColorQuickDraw() && ((**srcWindowP->portPixMap).pixelSize > 1))
{
// fill the sprite world with a pretty pattern
pixPatH = GetPixPat(kBreakOutBackDropPixPatID);
if (pixPatH != NULL)
{
FillCRect(&worldRect, pixPatH);
DisposePixPat(pixPatH);
}
else
{
FillRect(&worldRect, qd.ltGray);
}
}
else
{
FillRect(&worldRect, qd.ltGray);
}
SetPort((GrafPtr)srcWindowP);
//
// STEP #6: run the animation
//
HideCursor();
SWUpdateSpriteWorld(spriteWorldP);
while (!Button())
{
SWProcessSpriteWorld(spriteWorldP);
// did we lose the last ball?
if (ballSpriteP->userData == 4)
{
SysBeep(1);
break;
}
// see if the ball has hit the paddle
SWCollideSpriteLayer(paddleLayerP, ballLayerP);
// see if the ball has hit the bricks
SWCollideSpriteLayer(ballLayerP, brickLayerP);
SWAnimateSpriteWorld(spriteWorldP);
SystemTask();
}
//
// STEP #7: unlock the sprite world
//
SWUnlockSpriteWorld(spriteWorldP);
//
// STEP #8: dispose of the pieces we created
//
for (brickNum = 0; brickNum < kTotalNumberOfBricks; brickNum++)
{
SWDisposeSprite(brickSpriteArray[brickNum], brickNum == 0);
}
SWDisposeSprite(ballSpriteP, true);
SWDisposeSprite(paddleSpriteP, true);
SWDisposeSpriteLayer(brickLayerP);
SWDisposeSpriteLayer(ballLayerP);
SWDisposeSpriteLayer(paddleLayerP);
SWDisposeSpriteWorld(spriteWorldP);
//
// STEP #9: shut down the sprite world package
//
SWExitSpriteWorld();
FlushEvents(everyEvent, 0);
InvalRect(&srcWindowP->portRect);
ShowCursor();
SetCursor(&qd.arrow);
SysBeep(1);
}
void BallCollideProc(SpritePtr ballSpriteP, SpritePtr brickSpriteP, Rect* sectRect)
{
Rect bounceRect;
short inset;
if (brickSpriteP->isVisible)
{
SWSetSpriteVisible(brickSpriteP, false);
bounceRect = brickSpriteP->destFrameRect;
inset = 4;
bounceRect.left += inset;
bounceRect.right -= inset;
if ((ballSpriteP->destFrameRect.top >= bounceRect.bottom) ||
(ballSpriteP->destFrameRect.bottom <= bounceRect.top) ||
(ballSpriteP->destFrameRect.left >= bounceRect.right) ||
(ballSpriteP->destFrameRect.right <= bounceRect.left))
{
ballSpriteP->horizMoveDelta = -ballSpriteP->horizMoveDelta;
SWOffsetSprite(ballSpriteP, ballSpriteP->horizMoveDelta, 0);
}
else
{
ballSpriteP->vertMoveDelta = -ballSpriteP->vertMoveDelta;
SWOffsetSprite(ballSpriteP, 0, ballSpriteP->vertMoveDelta);
}
}
}
void PaddleCollideProc(SpritePtr paddleSpriteP, SpritePtr ballSpriteP, Rect* sectRect)
{
short ballDelta;
// is the paddle moving?
if (paddleSpriteP->destFrameRect.left != paddleSpriteP->oldFrameRect.left)
{
// apply some of the paddle’s movement to the ball
ballDelta = paddleSpriteP->destFrameRect.left - paddleSpriteP->oldFrameRect.left;
ballDelta /= 5;
if (ballDelta < 5)
{
ballSpriteP->horizMoveDelta += ballDelta;
}
}
ballSpriteP->vertMoveDelta = -ballSpriteP->vertMoveDelta;
SWOffsetSprite(ballSpriteP, 0, ballSpriteP->vertMoveDelta);
}
void PaddleMoveProc(SpritePtr paddleSpriteP, Point* spritePoint)
{
Point mousePoint;
GetMouse(&mousePoint);
if (mousePoint.h < paddleSpriteP->moveBoundsRect.left)
{
spritePoint->h = paddleSpriteP->moveBoundsRect.left;
}
else if (mousePoint.h > paddleSpriteP->moveBoundsRect.right)
{
spritePoint->h = paddleSpriteP->moveBoundsRect.right;
}
else
{
spritePoint->h = mousePoint.h;
}
}
void BallMoveProc(
SpritePtr srcSpriteP,
Point* spritePoint)
{
if (srcSpriteP->destFrameRect.left < srcSpriteP->moveBoundsRect.left)
{
srcSpriteP->horizMoveDelta = -srcSpriteP->horizMoveDelta;
spritePoint->h = srcSpriteP->moveBoundsRect.left;
}
else if (srcSpriteP->destFrameRect.right > srcSpriteP->moveBoundsRect.right)
{
srcSpriteP->horizMoveDelta = -srcSpriteP->horizMoveDelta;
spritePoint->h = srcSpriteP->moveBoundsRect.right -
(srcSpriteP->curFrameP->frameRect.right - srcSpriteP->curFrameP->frameRect.left);
}
if (srcSpriteP->destFrameRect.top < srcSpriteP->moveBoundsRect.top)
{
srcSpriteP->vertMoveDelta = -srcSpriteP->vertMoveDelta;
spritePoint->v = srcSpriteP->moveBoundsRect.top;
}
else if (srcSpriteP->destFrameRect.top > srcSpriteP->moveBoundsRect.bottom)
{
SysBeep(1);
srcSpriteP->userData++;
spritePoint->h = -20;
spritePoint->v = 200;
SWSetSpriteMoveDelta(srcSpriteP, 2, 2);
}
}